//
//  XYTButton.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/9/8.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTButton.h"

@implementation XYTButton

- (instancetype)initWithFrame:(CGRect)frame delegate:(id)delegate{
    if (self = [super initWithFrame:frame]) {
        self.delegate = delegate;
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    //手势
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickButton)];
    [self addGestureRecognizer:tapGesture];
    //imageView
    
    CGFloat imageHeight = boundsHeight-5-25;
    CGFloat imageWidth = imageHeight;
    CGFloat imageX = (boundsWidth-imageWidth)*0.5;
    CGFloat imageY = 5;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageWidth, imageHeight)];
    imageView.layer.cornerRadius = imageWidth*0.5;
    imageView.layer.masksToBounds = YES;
    [self addSubview:imageView];
    self.btnImage = imageView;
    //title
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageView.frame)+5, boundsWidth, 15)];
    titleLab.font = [UIFont systemFontOfSize:12];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [self addSubview:titleLab];
    self.titleLab = titleLab;
}

- (void)setTitle:(NSString *)title  image:(NSString *)imageName{
    self.titleLab.text = title;
    self.btnImage.image = [UIImage imageNamed:imageName];
}

- (void)onClickButton{
    if (self.delegate && [self.delegate respondsToSelector:@selector(xytButtonOnClick:)]) {
        [self.delegate xytButtonOnClick:self];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
